//
//  LXCarIdleListViewController.m
//  ZuLinProject
//
//  Created by bancheng on 2021/12/30.
//  Copyright © 2021 LX_App. All rights reserved.
//

#import "LXCarIdleListViewController.h"
#import "XiaJiaCheLiangListCell.h"
#import "LXCarLeaseApplyViewController.h"
@interface LXCarIdleListViewController ()<UITableViewDelegate,UITableViewDataSource,DZNEmptyDataSetSource, DZNEmptyDataSetDelegate>
@property (nonatomic, strong) UITableView       *tableView;
@property (nonatomic, strong) NSMutableArray    *dataAry;
@property (nonatomic, assign) NSInteger          pageIndex;
@property (nonatomic, assign) NSInteger          totalPage;
@property (nonatomic, assign) NSInteger          selectIndex;
@end

@implementation LXCarIdleListViewController

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self layoutCustomNav:self.view title:@"空闲车辆列表"];
    self.pageIndex = 1;
    if (kUserDefaultForBool(kRefreshStr)) {
        kUserDefaultSetBool(NO, kRefreshStr);
        [self.tableView.mj_header beginRefreshing];
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = White_Color;
    [self dataAry];
    self.userInfo = [YLUserMgr fetchUser];
    [self createTableView];
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.pageIndex = 1;
        [self loadData];
        
    }];
  
    [self.tableView.mj_header beginRefreshing];;
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        self.pageIndex++;
        [self loadData];
    }];
    
}
-(void)loadData{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setObject:@"1" forKey:@"type"];
    [params setObject:@"10" forKey:@"rows"];
    [params setObject:[NSString stringWithFormat:@"%ld",self.pageIndex] forKey:@"page"];

    
    [MSNetworkManager postRevertUrl:CarList_URL params:params success:^(id  _Nonnull paginationObj, id  _Nonnull resonseObj, BOOL isSuccess, NSString * _Nonnull message) {
        
        [self endRefreshing];
        
        if (isSuccess) {
            
            if ([self.tableView.mj_header isRefreshing]||self.pageIndex==1) {
                [self.dataAry removeAllObjects];
                [self.tableView.mj_footer resetNoMoreData];
            }
            
            
            NSArray *list = resonseObj[@"list"];
            [self.dataAry addObjectsFromArray:[CheLiangModel mj_objectArrayWithKeyValuesArray:list]];
            
            NSInteger totalPage = [resonseObj[@"total"] intValue];
            if (self.dataAry.count>=totalPage) {
                [self.tableView.mj_footer endRefreshingWithNoMoreData];
            }
            
            [self.tableView reloadData];
                        
            
        }
        
    } failure:^(NSUInteger code, NSString * _Nonnull message) {
        [self endRefreshing];
    } withShowHud:YES];
    
}
-(void)createTableView{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, LL_StatusBarAndNavigationBarHeight, ScreenWidth, B_HEIGHT - LL_StatusBarAndNavigationBarHeight - LL_TabbarSafeBottomMargin) style:UITableViewStyleGrouped];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.emptyDataSetSource = self;
    self.tableView.emptyDataSetDelegate = self;
    self.tableView.estimatedRowHeight = 0;
    self.tableView.estimatedSectionFooterHeight = 0;
    self.tableView.estimatedSectionHeaderHeight = 0;
    self.tableView.backgroundColor = White_Color;
    [self.tableView registerNib:[UINib nibWithNibName:[XiaJiaCheLiangListCell reuseIdentifier] bundle:nil] forCellReuseIdentifier:[XiaJiaCheLiangListCell reuseIdentifier]];
    [self.view addSubview:self.tableView];
    CGRect frame = CGRectMake(0, 0, 0, 0.1);
    self.tableView.tableHeaderView = [[UIView alloc] initWithFrame:frame];
}
#pragma mark - ------------------ DZNEmptyData ------------------
-(UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView{
    return [UIImage imageNamed:@"No_order_icon"];
}
- (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView {
    NSString *title = @"暂无数据";
    NSDictionary *attributes = @{NSFontAttributeName:[UIFont systemFontOfSize:15.0f],
                                 NSForegroundColorAttributeName:gray666_color
                                 };
    return [[NSAttributedString alloc] initWithString:title attributes:attributes];
}
- (void)emptyDataSet:(UIScrollView *)scrollView didTapView:(UIView *)view {
    if (!self.tableView.mj_header.isRefreshing && !self.tableView.mj_footer.isRefreshing) {
        [self.tableView.mj_footer resetNoMoreData];
        self.pageIndex = 1;
        [self loadData];
    }
}

- (CGFloat)verticalOffsetForEmptyDataSet:(UIScrollView *)scrollView {
    return -50;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return  self.dataAry.count;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
     return 1;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    XiaJiaCheLiangListCell *cell = [tableView dequeueReusableCellWithIdentifier:[XiaJiaCheLiangListCell reuseIdentifier]];
    if (!cell) {
        cell = [[XiaJiaCheLiangListCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:[XiaJiaCheLiangListCell reuseIdentifier]];
    }
    
    CheLiangModel *model = self.dataAry[indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.xiaJiaButton.hidden = YES;
    cell.rightImage.hidden = NO;
    cell.nameLb.text = model.carNum;
    cell.rightWidth.constant = 30;
    cell.chePaiLb.text = [NSString stringWithFormat:@"%@-%@",model.carBrand, model.carMode];
    return cell;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return [XiaJiaCheLiangListCell cellHeight];
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{


    LXCarLeaseApplyViewController *vc = [[LXCarLeaseApplyViewController alloc] init];
    vc.carModel = self.dataAry[indexPath.row];
    vc.type = 1;
    [self pushToNextController:vc];
    
    
}

#pragma mark ----------------<#note#>----------------
- (NSMutableArray *)dataAry {
    if (!_dataAry) {
        self.dataAry = [NSMutableArray array];
    }
    return _dataAry;
}

- (void)endRefreshing{
    if ([self.tableView.mj_header isRefreshing]) {
        [self.tableView.mj_header endRefreshing];
    }
    if ([self.tableView.mj_footer isRefreshing]) {
        [self.tableView.mj_footer endRefreshing];
    }

}

@end
